﻿Option Explicit On
Option Strict On

Imports Microsoft.VisualBasic

Public Class Utility
    Shared Function GeneratePassword(ByVal nNoChars As Integer, ByVal sValidChars As String) As String
        ' nNoChars = length of generated password
        ' sValidChars = valid characters. If zerolength-string ( "" ) then
        '     default is used: A-Z AND a-z AND 0-9

        Const szDefault As String = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789"
        Dim nCount As Integer = 0
        Dim sRet As String = ""
        Dim nNumber As Integer = 0
        Dim nLength As Integer = 0

        Randomize() 'init random 

        nLength = Len(sValidChars)
        If nLength <= 0 Then
            sValidChars = szDefault
        End If

        For nCount = 1 To nNoChars
            nNumber = CInt(Int((nLength * Rnd()) + 1))
            sRet = sRet & Mid(sValidChars, nNumber, 1)
        Next

        Return sRet
    End Function
    Shared Function PurgeString(ByVal OldStr As String) As String
        Dim NewStr As String = ""
        For l As Integer = 1 To Len(OldStr)
            If Asc(Mid(OldStr, l, 1)) > 31 Then
                NewStr = NewStr & Mid(OldStr, l, 1)
            End If
        Next
        Return NewStr
    End Function

    Shared Function GetDateTimeNumber() As String

        Dim sValue As String
        Dim dDate As Date = CDate(Date.Now.ToString())
        Dim sYear As String = CStr(Year(dDate))
        Dim sMnth As String = Right("0" + CStr(Month(dDate)), 2)
        Dim sDay As String = Right("0" + CStr(Day(dDate)), 2)
        Dim sHour As String = Right("0" + CStr(Hour(dDate)), 2)
        Dim sMin As String = Right("0" + CStr(Minute(dDate)), 2)
        Dim sSec As String = Right("0" + CStr(Second(dDate)), 2)
        sValue = sYear + sMnth + sDay + sHour + sMin + sSec
        Return sValue

    End Function
    Shared Function NewReferenceId() As String
        Dim nUpper As Integer = 9
        Dim nLower As Integer = 0
        Dim nOrgNo As Integer = 0
        'Dim nCount As Integer = 0
        'G23456FB
        Dim dTime As Date = Now()

        Dim iDay As Integer = Day(dTime)
        Dim iMonth As Integer = Month(dTime)
        Dim iHrs As Integer = Hour(dTime)
        Dim iMins As Integer = Minute(dTime)
        Dim iSecs As Integer = Second(dTime)
        Dim iYear As Integer = Year(dTime)

        Dim sDays() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE"}
        Dim sMonths() As String = {"JN", "FB", "MA", "AP", "MY", "JU", "JL", "AG", "SP", "OC", "NV", "DC"}

        'Generate 3 random characters
        Dim char1 As Char = Chr(64 + CInt(26 * Rnd() + 1))
        Dim char2 As Char = Chr(64 + CInt(26 * Rnd() + 1))
        Dim char3 As Char = Chr(64 + CInt(26 * Rnd() + 1))

        'Generate 5 random digits
        Randomize()
        Dim digit1 As Integer = CInt(9 * Rnd() + 1)
        Randomize()
        Dim digit2 As Integer = CInt(9 * Rnd() + 1) * 10
        Randomize()
        Dim digit3 As Integer = CInt(9 * Rnd() + 1) * 100
        Randomize()
        Dim digit4 As Integer = CInt(9 * Rnd() + 1) * 1000
        Randomize()
        Dim digit5 As Integer = CInt(9 * Rnd() + 1) * 10000

        nOrgNo = digit5 + digit4 + digit3 + digit2 + digit1

        Return sDays(iDay - 1).ToString + nOrgNo.ToString + sMonths(iMonth - 1).ToString

    End Function

End Class
